package com.bootdo.collector.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.bootdo.collector.utils.ESParse;
import com.bootdo.screen.domain.TypetruenumDO;
import com.bootdo.screen.service.TypetruenumService;
import org.apache.http.HttpEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.nio.entity.NStringEntity;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.GregorianCalendar;

public class TypeCountBYMonthJob implements Job {

    @Autowired
    private TypetruenumService typetruenumService;

    @Autowired
    private RestClient restClient;

    private final String ENERGYSEARCHURL ="energy_search/_search?size=0";
    private final String TYPESEARCHSTRING ="{ \"query\": {\"range\": { \"crawl_time\": { \"gte\": %d, \"lte\": %d } }}, \"aggs\": { \"types\": { \"terms\": {\"field\": \"file_type\"} } } }";

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        Calendar calendar  =   new GregorianCalendar();
        Long now = calendar.getTimeInMillis()/1000;
        calendar.set( Calendar.DATE,  1 );
        calendar.set(Calendar.HOUR,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        Long month = calendar.getTimeInMillis()/1000;
        String query = String.format(TYPESEARCHSTRING,month,now);
        String res = ESParse.execQuery(restClient,query, ENERGYSEARCHURL);

        JSONArray arr = JSON.parseObject(res).getJSONObject("aggregations").getJSONObject("types").getJSONArray("buckets");
        TypetruenumDO typeNum = new TypetruenumDO();

        typeNum.setPdf(0l);
        typeNum.setTxt(0l);
        typeNum.setXls(0l);
        typeNum.setDoc(0l);
        typeNum.setHtml(0l);
        typeNum.setPpt(0l);

        for (int i=0 ;i<arr.size();i++) {
           if (arr.getJSONObject(i).getString("key").equals("html")) {
               typeNum.setHtml(arr.getJSONObject(i).getLong("doc_count"));
           } else if (arr.getJSONObject(i).getString("key").equals("doc")) {
               typeNum.setDoc(arr.getJSONObject(i).getLong("doc_count"));
           } else if (arr.getJSONObject(i).getString("key").equals("pdf")) {
               typeNum.setPdf(arr.getJSONObject(i).getLong("doc_count"));
           } else if (arr.getJSONObject(i).getString("key").equals("xls") || arr.getJSONObject(i).getString("key").equals("xlsm")) {
               typeNum.setXls( typeNum.getXls() + arr.getJSONObject(i).getLong("doc_count"));
           } else if (arr.getJSONObject(i).getString("key").equals("txt")) {
               typeNum.setTxt(arr.getJSONObject(i).getLong("doc_count"));
           } else if (arr.getJSONObject(i).getString("key").equals("pdf")) {
               typeNum.setPdf(arr.getJSONObject(i).getLong("doc_count"));
           }
        }
        Date date1 = new Date();
        date1.setTime(month * 1000);
        typeNum.setDate(date1);
        typetruenumService.saveUpdate(typeNum);
    }
}
